﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace sdamarketing.Marketing
{
    public partial class Reklama : osnova1
    {

        reklamaEditor reklama;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            postaviVrednosti();
         
                   

            String cekor;
            if (Request["step"] == null)
                cekor = "0";
            else
                cekor = Request["step"].ToString();

            
            if(cekor.Equals("0"))
            {
                reklama = new reklamaEditor();
                Session["Reklama"] = reklama;
            }
            else 
            {
                reklama = (reklamaEditor) Session["Reklama"];
            }
            reklama.setUserId(Int32.Parse(Session["idUser"].ToString())); 

            String nacin;
            if (Request["nacin"] == null)
                nacin = "-1";
            else
                nacin = Request["nacin"].ToString();

            switch (cekor)
            {
                case "0":
                    voved.Visible = true;
                    step1.Visible = false;
                    step2.Visible = false;
                    step3users.Visible = false;
                    step3keywords.Visible = false;
                    previewPanel.Visible = false;
                    UspehPanel.Visible = false;
                    break;

                case "1":
                    voved.Visible = false;
                    step1.Visible = true;
                    step2.Visible = false;
                    step3users.Visible = false;
                    step3keywords.Visible = false;
                    previewPanel.Visible = false;
                    UspehPanel.Visible = false;
                    break;

                case "2":
                    voved.Visible = false;
                    step1.Visible = false;
                    step2.Visible = true;
                    step3users.Visible = false;
                    step3keywords.Visible = false;
                    previewPanel.Visible = false;
                    UspehPanel.Visible = false;
                    break;
                case "3":
                    voved.Visible = false;
                    step1.Visible = false;
                    step2.Visible = false;
                    if (nacin == "0")
                    {
                        step3users.Visible = true;
                        step3keywords.Visible = false;
                    }
                    if (nacin == "1")
                    {
                        step3users.Visible = false;
                        step3keywords.Visible = true;
                    }
                    previewPanel.Visible = false;
                    UspehPanel.Visible = false;
                    break;
                case "4":
                    voved.Visible = false;
                    step1.Visible = false;
                    step2.Visible = false;
                    step3users.Visible = false;
                    step3keywords.Visible = false;
                    previewPanel.Visible = true;
                    naslovLink.Text = reklama.getNaslov();
                    naslovLink.NavigateUrl = reklama.getLink();
                    slikaLink.ImageUrl = reklama.getSlikaURL();
                    slikaLink.NavigateUrl = reklama.getLink();
                    opisLink.Text = reklama.getOpis();
                    opisLink.NavigateUrl = reklama.getLink();

                    UspehPanel.Visible = false;
                    break;
                case "success":
                    voved.Visible = false;
                    step1.Visible = false;
                    step2.Visible = false;
                    step3users.Visible = false;
                    step3keywords.Visible = false;
                    previewPanel.Visible = false;
                    UspehPanel.Visible = true;
                    break;

                default:
                    break;
            }

        }

        protected void postaviVrednosti()
        {
            for (int i = 1; i <= 150; i++)
            {
                reklama = (reklamaEditor)Session["Reklama"];
                vozrastOd.Items.Add(i.ToString());
                vozrastDo.Items.Add(i.ToString());
                Session["Reklama"] = reklama;
            }
        }

        protected void btPostavi_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Marketing/Reklama.aspx?step=1");
        }

        protected void btProdolzi1_Click(object sender, EventArgs e)
        {
            reklama = (reklamaEditor)Session["Reklama"];
            int nacin = Int32.Parse(izborNacin.SelectedValue.ToString());
            if (nacin != -1)
            {
                reklama.setNacin(nacin);
                Session["Reklama"] = reklama;
                Response.Redirect("~/Marketing/Reklama.aspx?step=2&nacin=" + nacin);
            }

        }

        public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
        {
            System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);

            // Prevent using images internal thumbnail
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

            if (OnlyResizeIfWider)
            {
                if (FullsizeImage.Width <= NewWidth)
                {
                    NewWidth = FullsizeImage.Width;
                }
            }

            int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
            if (NewHeight > MaxHeight)
            {
                // Resize with height instead
                NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
                NewHeight = MaxHeight;
            }

            System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

            // Clear handle to original file so that we can overwrite it if necessary
            FullsizeImage.Dispose();

            // Save resized picture
            NewImage.Save(NewFile);


        }

        protected void btProdolzi2_Click(object sender, EventArgs e)
        {
            reklama = (reklamaEditor)Session["Reklama"];
            String appPath = Request.PhysicalApplicationPath;

            if (reklama.getSlikaURL() == null)
                reklama.setSlikaURL(/*appPath + */"\\sliki\\person.png");
            

            int nacin = Int32.Parse(Request["nacin"].ToString());
            if (nacin != -1)
            {
                reklama.setNaslov(tbNaslov.Text);
                reklama.setOpis(tbOpis.Text);
                reklama.setLink(http.SelectedValue + tbLink.Text);
                Session["Reklama"] = reklama;
                Response.Redirect("~/Marketing/Reklama.aspx?step=3&nacin=" + nacin);
            }


        }

        protected void btUpload_Click(object sender, EventArgs e)
        {

            // Get the physical file system path for the currently
            // executing application.
            String appPath = Request.PhysicalApplicationPath;
            String userID = Session["idUser"].ToString();
            String saveDir = @"\Uploads\" + userID + "\\";

            System.IO.Directory.CreateDirectory(appPath + "\\Uploads\\" + userID);

            // Before attempting to save the file, verify
            // that the FileUpload control contains a file.
            if (FileUpload1.HasFile)
            {
                // Get the name of the file to upload.
                String fileName = Server.HtmlEncode(FileUpload1.FileName);

                // Get the extension of the uploaded file.
                String extension = System.IO.Path.GetExtension(fileName);

                // Get the size in bytes of the file to upload.
                int fileSize = FileUpload1.PostedFile.ContentLength;

                // Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded.
                if (fileSize < 2100000 && ((extension.ToUpper() == ".JPG") | (extension.ToUpper() == ".JPEG")))
                {

                    String savePath = appPath + saveDir +
                    Server.HtmlEncode(FileUpload1.FileName);

                    // Call the SaveAs method to save the 
                    // uploaded file to the specified path.
                    // This example does not perform all
                    // the necessary error checking.               
                    // If a file with the same name
                    // already exists in the specified path,  
                    // the uploaded file overwrites it.
                    FileUpload1.SaveAs(savePath);

                    // namali ja prikacenata slika na max 120 visina ili sirina
                    ResizeImage(savePath, savePath, 120, 120, false);

                    reklama.setSlikaURL(saveDir + Server.HtmlEncode(FileUpload1.FileName));//savePath);

                    // Notify the user that the file was uploaded successfully.
                    UploadStatusLabel.Text = "Your file was uploaded successfully.";

                }
                else
                {
                    // Notify the user why their file was not uploaded.
                    UploadStatusLabel.Text = "Your file was not uploaded because " +
                                         "it exceeds the 2 MB size limit OR it does not have a .jpg or .jpeg extension.";
                }

            }
            else
            {
                // Notify the user that a file was not uploaded.
                UploadStatusLabel.Text = "You did not specify a file to upload.";

            }
        }


        protected void btProdolzi3pregled_Click(object sender, EventArgs e)
        {
            reklama = (reklamaEditor)Session["Reklama"];
            int nacin = Int32.Parse(Request["nacin"].ToString());
            if (nacin == 0)
            {
                reklama.setLokacija(lokacija.SelectedValue);
                reklama.setVozrastOd(Int32.Parse(vozrastOd.SelectedValue));
                reklama.setVozrastDo(Int32.Parse(vozrastDo.SelectedValue));
                reklama.setPol(pol.SelectedValue);
            }
            if (nacin == 1)
            {
                reklama.setKlucniZborovi(tbKeywords.Text);
            }

            Session["Reklama"] = reklama;
            Response.Redirect("~/Marketing/Reklama.aspx?step=4");
            

        }

        protected void btProdolzi3pregled4_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Marketing/Reklama.aspx?step=0");
            
        }

        protected void btProdolzi3pregled3_Click(object sender, EventArgs e)
        {
            reklama = (reklamaEditor) Session["Reklama"];
            reklama.insertReklama();
            Response.Redirect("~/Marketing/Reklama.aspx?step=success");
        }

  
        protected void slikaReklama_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect(reklama.getLink(), false);
        }




    }
}
